



function formCheck() {
    return $('#editForm').validate({
        rules: {
            name: {
                required: true
            },
            code: {
                required: true
            },
            description: {
                required: true
            }
        }
    });
}


function buildButton(type) {
    var btnTemplate = {
        className: "btn2" == type ? "btn-default" : "btn-info"
    };
    var labelObject = {"btn0": "保存", "btn1": "保存继续", "btn2": "关闭", "btn3": "保存"};
    btnTemplate.label = labelObject[type];
    btnTemplate.callback = function () {
        if ("btn2" == type) {
            return true;//关闭
        }
        if (!formCheck().form()) {
            return false;
        }
        var $form = $("#editForm");
        if ("btn0" == type || "btn1" == type) {
            $.getJSON($form.prop("action"), $form.serialize(), function (data) {
                $.messager.alert0({selector: "#editAlert", type: data.status, html: data.body});
                if (data && data.status == "SUCCESS") {
                    if ("btn0" == type) {
                        treetable.reload();
                        $.bootbox.hideAll();
                    }
                }
            });
        }
        else if ("btn3" == type) {
            $.getJSON(ctx + "/area/update", $form.serialize(), function (data) {
                $.messager.alert0({selector: "#editAlert", type: data.status, html: data.body});
                if (data && data.status == "SUCCESS") {
                    treetable.reload();
                    $.bootbox.hideAll();
                }
            });
        }
        return false;
    }
    return btnTemplate;

}


function doEdit(id) {
    var url = ctx + "/area/edit_none?id=" + id + "&type=edit";
    var title = "地区编辑";
    var buttons = {
        btn0: buildButton("btn3"),//编辑时候的保存按钮
        btn2: buildButton("btn2")
    };
    $.messager.dialog({url: url, title: title, buttons: buttons});
}

function doAdd(id) {
    var url = ctx + "/area/edit_none?id=" + id + "&type=add";
    var title = "地区增加";
    var buttons = {
        btn0: buildButton("btn0"),
        btn1: buildButton("btn1"),
        btn2: buildButton("btn2")
    };
    $.messager.dialog({url: url, title: title, buttons: buttons});
}
/**
 *删除
 */
function doDelete(id) {
    if (!id) {
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    var childs = $('#' + id).attr("hasChild");
    if ("true" == childs) {
        $.messager.alert("请先删除下级数据");
        return;
    }
    var url = ctx + "/area/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if (ck) {
            var ary = [];
            ary.push(id);
            $.getJSON(url, {ids: ary}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.status, html: data.body});
                if (data && data.status == 'SUCCESS') {
                    treetable.reload();
                }
            });
        }
    });
}

/**查询*/
function doSearch(){
    var name=$("input[name=name]").val();
    var status=$("select[name=status]").val();
    var obj={"name":name,"status":status};
    treetable.params=obj;
    treetable.reload();

}
